import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';

class LogHelper {
  static Logger? _logger;
  LogHelper.d(dynamic message, {String title = ''}) {
    _logger ??= Logger(
      printer: PrettyPrinter(
          methodCount: 2, // Number of method calls to be displayed
          errorMethodCount:
              8, // Number of method calls if stacktrace is provided
          lineLength: 120, // Width of the output
          colors: true, // Colorful log messages
          printEmojis: true, // Print an emoji for each log message
          printTime: true // Should each log print contain a timestamp
          ),
    );

    if (kDebugMode) {
      String track = StackTrace.current.toString();
      List<String> infos = track.split("#")..remove("");
      if (infos.length >= 2) {
        print("#${infos[1].replaceAll(" ", "")}");
      }
    }
    if (title.isNotEmpty) {
      _logger!.d('''
=======🏀  $title  🏀=========
\n  $message
      ''');
    } else {
      _logger!.d(message);
    }
    debugPrint("\n");
  }
}

extension ObjectLogger on Object {
  Object d({String title = ''}) {
    LogHelper.d(this, title: title);
    return this;
  }
}
